<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
    <el-col :span="20">
      <span>{{ nodeData.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ nodeData.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="handleAction">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!nodeData.root" command="edit">编辑部分</el-dropdown-item>
              <el-dropdown-item v-if="!nodeData.root" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { reqDelDepartments } from '@/api/departments'

export default {
  name: 'TreeTools',
  props: {
    nodeData: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    handleAction (type) {
      // console.log(type)
      if (type === 'add') {
        // 添加部门,显示弹窗,带着id
        this.$emit('toggle-box', {
          isShow: true,
          type: 'add',
          pid: this.nodeData.id
        })
      } else if (type === 'edit') {
        // 编辑部门
        this.$emit('toggle-box', {
          isShow: true,
          type: 'edit',
          pid: this.nodeData.id
        })
      } else if (type === 'del') {
        // 删除部门
        this.$confirm('确认要删除吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 确定删除//如果这里的return的是Promise对象,那么下一个.then得到他的结果
          return reqDelDepartments(this.nodeData.id)
        }).then((ret) => {
          // 删除成功,提示一下
          if (ret.code === 10000) {
            this.$message.success('删除成功')
            // 刷新列表，通知父组件刷新列表
            this.$emit('on-success')
          }
        }).catch(err => {
          console.log(err)
          // 如果点击取消,他的值是cancel
          if (err !== 'cancel') {
            this.$message.error('删除异常')
          }
        })
      }
    }
  }
}
</script>

<style>
</style>
